<template>
	<m-layout title="我的分公司列表">
		<view class="content">
			<m-block gap="30" spa="0 0 20" v-for="(item,index) in listArr" class="listBox" :key="index">
				<!-- 1 正常  2  失效   3 待审核   4  驳回 -->
				<view class="flex fs21 mb-8">
					<text>状态：</text>
					<text  class="text-primary" v-if="item.state=='1'">有效</text>
					<text  class="text-red"  v-if="item.state=='2'">无效</text>
					<text  class="text-red" v-if="item.state=='3'">待审核</text>
					<text  class="text-red"  v-if="item.state=='4'">驳回</text>
				</view>
				<text class="fs24 tag"
					:class="[item.compyuserid==item.userid?'tag22':'']">{{item.compyuserid==item.userid?'公司发起人':'公司合伙人'}}</text>
				<view class="flex-between">
					<view class="flex">
						<m-icon size="36" spa="0 10 0 0" color="#887dff" type="icon-gongsimingcheng"  />
						<text class="otp2 fs30 fw-600 " style="max-width: 450rpx;">{{item.compyname}}</text>
					</view>
				</view>
				<view class="fs24 mt-8">
					<text>创建时间：</text>
					<text>{{item.ctime.replace("T"," ")}}</text>
				</view>
				<view class="fs24 mt-8" v-if="item.compytruname">
					<text>公司法人：</text>
					<text>{{item.compytruname}}</text>
				</view>
				<view class="fs24 mt-16">
					<view class="flex-between">
						<text class="fs26 fw-600">我的礼包收货地址</text>
						<m-button class="mr-12 plr-48" size="small" type="hollow"
							@click="selectedIdx=item.idx;Jump('/pages/mine/address/address?isSet=true')">{{item.truename?'修改':'设置'}}</m-button>
					</view>
					<m-block bgColor="#eeedfa" gap="16" spa="12 0">
						<template v-if="item.truename">
							<view class="">
								<text>收货人：</text>
								<text>{{item.truename}}</text>
							</view>
							<view class="mtb-4">
								<text>联系方式：</text>
								<text>{{item.phone}}</text>
							</view>
							<view class="">
								<text>收货地址：</text>
								<text>{{item.addr}}</text>
							</view>
						</template>
						<template v-else>
							<view class="">
								暂无收货地址
							</view>
						</template>
					</m-block>
				</view>
				<view class="flex-between mt-16">
					<text class="fs26 fw-600">我的礼包发货列表</text>
					<m-button class="mr-12 plr-48" size="small" type="hollow"
						@click="Jump(`/pages/mine/myCompyName/shopList?idx=${item.idx}`)">查看</m-button>
				</view>
				<view class="flex-between mt-16" v-if="item.compyuserid==item.userid">
					<view class="flex text-primary" @click="Jump(`/pages/mine/myCompyName/compyDetail?compyid=${item.compyid}`)">
						<text class="fs24">公司详情及成员</text>
						<m-icon size="24" color="text-primary" type="icon-qianjin" />
					</view>
					<view class="flex text-primary mt-12" @click="Jump(`/pages/mine/myCompyName/myteamCompy?compyid=${item.compyid}`)">
						<text class="fs24">下级分公司列表</text>
						<m-icon size="24" color="text-primary" type="icon-qianjin" />
					</view>
				</view>
			</m-block>
			<m-empty :loading="isLoading" v-if="listArr.length<=0" spa="120 0" title="未查询到数据" size="300"
				icon='empty'></m-empty>
		</view>
		<m-dialog title="提示" :loading="btnLoading" :showCancel="true" :isShow="setAddressShow" @handleCancel="setAddressCloseFn
			" @handleConfirm="setAddressFn">
			<view class="ta-center ptb-36 flex-column">
				<text class="mb-24">确定设置该地址为礼包收货地址？</text>
				<m-block bgColor="#eeedfa" gap="16" spa="12 0" class="ta-left fs24"
					v-if="addressObj&&addressObj[nowId]">
					<view class="flex">
						<text>收货人：</text>
						<text>{{addressObj[nowId]['username']}}</text>
					</view>
					<view class="mtb-4 flex">
						<text>联系方式：</text>
						<text>{{addressObj[nowId]['userphone']}}</text>
					</view>
					<view class="flex ai-flex-start">
						<text class="ws-nowrap">收货地址：</text>
						<text>{{addressObj[nowId]['are']}}{{addressObj[nowId]['useraddress']}}</text>
					</view>

				</m-block>
			</view>
		</m-dialog>
	</m-layout>
</template>

<script>
	import { mapState, mapActions } from 'vuex';
	import { hhrpage, hhrwaybillpage, hhrpageFromCompy,myhhrorder_setaddr } from '@/api/Users.js';
	export default {
		name: 'myCompyName',
		data() {
			return {
				selectedIdx: 0,
				pageIndex: 1, // 当前页数
				pageSize: 10,
				pageCount: 1, // 列表总页数（用于加载更多）
				listArr: [], // 主列表
				isLoading: false,
				setAddressShow: false,
				btnLoading: false,
			}
		},
		computed: {
			...mapState({
				nowId: state => state.address.nowId,
				haveSelected: state => state.address.haveSelected,
				addressObj: state => state.address.addressObj,
			}),
		},
		watch: {
			haveSelected(newVal) {
				console.log('newVal', newVal)
				if (newVal) {
					this.setAddressShow = true;
				}
			}
		},
		onLoad() {
			this.$store.commit('address/SET_HAVESELECTEN', false)
		},
		onShow() {

			this.initData();
		},
		onPullDownRefresh() {
			this.initData();
		},
		onReachBottom() {
			if (this.pageIndex < this.pageCount) {
				this.pageIndex += 1;
				this.getList();
			}
		},
		methods: {
			///////////////////////////////////// 初始化 ////////////////////////////////////////
			initData() {
				this.isEditor = false;
				this.pageIndex = 1;
				this.pageSize = 10;
				this.pageCount = 1;
				this.listArr = [];
				this.getList();
			},
			///////////////////////////////////// 网络请求 ////////////////////////////////////////
			// 获取主列表
			async getList() {
				if (this.pageIndex > this.pageCount) {
					return
				}
				this.isLoading = true;
				const { list, pageCount } = await hhrpage({
					pageSize: this.pageSize,
					pageIndex: this.pageIndex,
				});
				this.isLoading = false;
				this.pageCount = pageCount;
				if (this.pageIndex <= 1) {
					this.listArr = list;
				} else {
					this.listArr = [...this.listArr, ...list];
				}
				this.pageIndex++;
				uni.stopPullDownRefresh();
			},
			setAddressCloseFn() {
				this.$store.commit('address/SET_HAVESELECTEN', false)
				this.setAddressShow = false;
				this.btnLoading = false;
			},
			// 设置地址
			async setAddressFn() {
				this.btnLoading = true;
				const { success, msg } = await myhhrorder_setaddr({
					idx: this.selectedIdx,
					phone: this.addressObj[this.nowId]['userphone'],
					truename: this.addressObj[this.nowId]['username'],
					addr: this.addressObj[this.nowId]['are'] + ' ' + this.addressObj[this.nowId]['useraddress']
				})
				this.btnLoading = false;
				this.$store.commit('address/SET_HAVESELECTEN', false)
				if (!success) {
					this.Toast(msg);
				} else {
					this.setAddressShow = false;
					this.initData();
				}
			},
			onPullDownRefresh() {
				this.initData();
			},
			onReachBottom() {
				this.getList();
				console.log('触底了');
			}
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 24rpx;
	}

	.listBox {
		position: relative;
	}

	.tag {
		position: absolute;
		right: 0rpx;
		top: 0rpx;
		padding: 12rpx 32rpx;
		color: #FFFFFF;
		background-color: #786BFF;
		font-size: 21rpx;
		border-radius: 0rpx 20rpx 0rpx 24rpx;
		position: absolute;
	}

	.tag22 {
		background-color: #826a4a;
	}
</style>